package com.guang.springbootspringmvcorigin.config;

import com.guang.springbootspringmvcorigin.filter.MyFilter;
import com.guang.springbootspringmvcorigin.interceptor.MyInterceptor;
import com.guang.springbootspringmvcorigin.listener.MyWebListener;
import com.guang.springbootspringmvcorigin.servlet.MyServlet;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Arrays;

@Configuration(proxyBeanMethods = true)
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MyInterceptor());
    }

    @Bean
    public ServletRegistrationBean myServlet(){
        ServletRegistrationBean<MyServlet> myServletServletRegistrationBean = new ServletRegistrationBean<>(new MyServlet(), "/my", "/my01");
        return myServletServletRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean myFilterRegistrationBean(){
        FilterRegistrationBean<MyFilter> myFilterFilterRegistrationBean = new FilterRegistrationBean<>(new MyFilter(), myServlet());
        myFilterFilterRegistrationBean.setUrlPatterns(Arrays.asList("/*"));
        return myFilterFilterRegistrationBean;
    }


    @Bean
    public ServletListenerRegistrationBean myServletListenerRegistrationBean(){
        ServletListenerRegistrationBean<MyWebListener> myFilterFilterRegistrationBean = new ServletListenerRegistrationBean<MyWebListener>(new MyWebListener());
        return myFilterFilterRegistrationBean;
    }

}
